<?php

namespace app\index\controller;
use app\index\Controller;
use think\Db;
use think\Validate;
use think\Config;
/**
 * @title 押金(模块03)
 * @description 接口说明
 * @group 押金
 * @header name:model_id   default:03 desc:模块id
 * @groupremark 已使用错误码：1030001 1030002
 */
class Deposit extends  Controller{
    /**
     * @title 押金信息
     * @description 接口说明
     * @author 梁敏
     * @url /index/deposit/index
     * @method ~
     *
     *
     * @param name:token type:string require:1
     * @param name:user_id type:string require:1 用户id
     * @return data:押金信息@!
     * @object deposit:押金金额 deposit_status:押金状态notpay未交付,paid已交付,apply_refund申请退款,refunded已退款 deposit_time:押金交付时间 isdeposit:是否已交押金
     * @return error_id: 1030001 校验错误
     */
    public function index() {
        //接口代码
        $data=input();

        $vmsg = [
            'user_id.require'   => '未指定用户',
        ];
        $validate = new Validate([
            'token'  => 'require',
            'user_id'=>"require",
        ],$vmsg);

        if (!$validate->check($data)) {
            $msg['error_id']="1030001";
            $msg['message']=$validate->getError();
            return $this->error($msg);
        }
        $info=Db::name("admin_user")->where('id',$data['user_id'])->find();
        if($info){
            $deposit['deposit']=$info['deposit'];
            $deposit['deposit_status']=$info['deposit_status'];
            $deposit['isdeposit']=$info['isdeposit'];
            $deposit['deposit_time']=$info['deposit_time'];




            $msg['message']="获取成功";
            $msg['data']=$deposit;
            return $this->success($msg);
        }else{
            $msg['error_id']="1100002";
            $msg['message']="留言失败";
            return $this->error($msg);
        }
    }

    /**
     * @title 申请退押金
     * @description 接口说明
     * @author 梁敏
     * @url /index/deposit/refund_apply
     * @method ~
     * @param name:token type:string require:1
     * @param name:refund_item type:string require:1 desc:refund_item（例：押金,GPS）
     * @param name:reason type:string require:1 desc:reason（最长255个字符）
     * @param name:deposit type:string require:1 desc:退款金额
     * @return error_id: 1030002校验失败,1030003押金不存在,1030004正在申请,1030005申请失败
     */
    public function refund_apply() {
        //接口代码
        $data = input();
        $data['user_id'] = UID;
        $vmsg = [
            'token.require' => '缺少token',
            'user_id.require'   => '用户未授权',
            'refund_item.require'   => '请选择退款项目',
            'reason.require'   => '请选择申请原因',
            'reason.max'   => '申请原因不可超过255个字符',
        ];
        $validate = new Validate([
            'token'  => 'require',
            'user_id'=>"require",
            'refund_item'=>"require",
            'reason'=>"require|max:255",
        ],$vmsg);

        if (!$validate->check($data)) {
            $msg['error_id'] = "1030002";
            $msg['message'] = $validate->getError();
            return $this->error($msg);
        }

        $info = Db::name("admin_user")->where('id',$data['user_id'])->find();
        if (!$info || !$info['isdeposit']) {
            $msg['error_id'] = "1030003";
            $msg['message'] = "没有押金";
            return $this->error($msg);
        }
        if ($info['deposit_status'] == 'apply_refund') {
            $msg['error_id'] = "1030004";
            $msg['message'] = "正在申请";
            return $this->error($msg);
        }

        $refund_item = explode(',',$data['refund_item']);

        $item = ['押金','GPS'];
        foreach ($refund_item as $rk => $rv){
            if(!in_array($rv,$item)){
                $msg['message'] = "不存在“".$rv.'”的收款项目，无法进行申请退押金';
                return $this->error($msg);
            }
        }

        //查询押金记录
        $driver_deposit = Db::name('driver_deposit')->where('driver_id',$data['user_id'])->find();

        if(!$driver_deposit){
            $msg['error_id'] = "1030003";
            $msg['message'] = "不存在押金记录";
            return $this->error($msg);
        }

        if(empty($driver_deposit['collection_item'])){
            $msg['error_id'] = "1030003";
            $msg['message'] = "无收款项目记录";
            return $this->error($msg);
        }

        // 启动事务
        Db::startTrans();
        try{
            $update['deposit_status'] = 'apply_refund';
            $re = Db::name("admin_user")->where('id',$data['user_id'])->update($update);
            $insert['user_id'] = $data['user_id'];
            $insert['money'] = $data['deposit'];
            $insert['create_time'] = NOW_TIME;
            $re1 = Db::name("deposit_refund_apply")->insertGetId($insert);
            $res2 = Db::name('driver_deposit')->where('driver_id',$data['user_id'])->update(array('refund_item'=>$data['refund_item'],'reason'=>$data['reason'],'refund_status'=>4));
            if($re && $re1 && $res2){
                $msg['message']="申请成功";
                Db::commit();
                return $this->success($msg);
            }else{
                $msg['error_id']="1030005";
                $msg['message']="申请失败";
                Db::rollback();
                return $this->error($msg);
            }
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $msg['error_id']="1030005";
            $msg['message']="申请失败";
            return $this->error($msg);
        }
    }

    /**
     * @title 获取申请退押金信息
     * @description 接口说明
     * @author YDG
     * @url /index/deposit/refund_apply_info
     * @method ~
     * @param name:token type:string require:1 desc:token
     * @return error_id: 1030002校验失败,1030003押金不存在,1030004正在申请,1030005申请失败
     */
    public function refund_apply_info() {
        //接口代码
        $data = input();
        $data['user_id'] = UID;
        $vmsg = [
            'token.require' => '缺少token',
            'user_id.require'   => '用户未授权',
        ];
        $validate = new Validate([
            'token'  => 'require',
            'user_id'=>"require",
        ],$vmsg);

        if (!$validate->check($data)) {
            $msg['error_id'] = "1030002";
            $msg['message'] = $validate->getError();
            return $this->error($msg);
        }

        $info = Db::name("admin_user")->where('id',$data['user_id'])->find();
        if (!$info || !$info['isdeposit']) {
            $msg['error_id'] = "1030003";
            $msg['message'] = "没有押金";
            return $this->error($msg);
        }
        if ($info['deposit_status'] == 'apply_refund') {
            $msg['error_id'] = "1030004";
            $msg['message'] = "正在申请";
            return $this->error($msg);
        }

        //查询押金记录
        $driver_deposit = Db::name('driver_deposit')->where('driver_id',$data['user_id'])->find();

        if(!$driver_deposit){
            $msg['error_id'] = "1030003";
            $msg['message'] = "不存在押金记录";
            return $this->error($msg);
        }

        if(empty($driver_deposit['collection_item'])){
            $msg['error_id'] = "1030003";
            $msg['message'] = "无收款项目记录";
            return $this->error($msg);
        }

        $collection_item = explode(',',$driver_deposit['collection_item']);
        $msg['message'] = "获取成功";
        $msg['data'] = $collection_item;
        return $this->success($msg);
    }
}
